import numpy as np

S_INFO = 6  # bit_rate, buffer_size, next_chunk_size, bandwidth_measurement(throughput and time), chunk_til_video_end
S_LEN = 8  # take how many frames in the past
A_DIM = 4  # Action dimension


class tile:
    def __init__(self, tile_id, seg_size, total_tiles):
        if total_tiles == 6:
            FACE_COORD = [[1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, -1], [0, 0, 1]]
        else:
            FACE_COORD = [[1, 0.5, -0.5], [1, 0.5, 0.5], [1, -0.5, -0.5], [1, -0.5, 0.5],
                          [-1, 0.5, 0.5], [-1, 0.5, -0.5], [-1, -0.5, 0.5], [-1, -0.5, -0.5],
                          [-0.5, 1, 0.5], [0.5, 1, 0.5], [-0.5, 1, -0.5], [0.5, 1, -0.5],
                          [-0.5, -1, -0.5], [0.5, -1, -0.5], [-0.5, -1, 0.5], [0.5, -1, 0.5],
                          [-0.5, 0.5, -1], [0.5, 0.5, -1], [-0.5, -0.5, -1], [0.5, -0.5, -1],
                          [0.5, 0.5, 1], [-0.5, 0.5, 1], [0.5, -0.5, 1], [-0.5, -0.5, 1]]
        self.tile_id = tile_id
        self.seg_size = seg_size
        self.seg_count = len(seg_size)
        # self.seg_duration = seg_duration
        self.face_coord = FACE_COORD[tile_id]
        self.a_batch = []
        self.s_batch = []
        self.r_batch = []
        self.entropy = []
        self.old_pi_batch = []
        self.deviation = 0
        self.last_deviation = 0
        self.quality = 0
        self.last_quality = 0
        self.action_vec = np.zeros(A_DIM)
        self.action_vec[self.quality] = 1
        self.state = np.zeros((S_INFO, S_LEN))

    def update_state(self, state_):
        self.state = np.roll(self.state, -1, axis=1)
        self.state[0, -1] = state_[0]   # deviation
        self.state[1, -1] = state_[1]   # throughput KBps
        self.state[2, -1] = state_[2]   # download time s
        self.state[3, 0] = state_[3]    # next size KB
        self.state[3, 1] = state_[4]    # next size KB
        self.state[3, 2] = state_[5]    # next size KB
        self.state[3, 3] = state_[6]    # next size KB
        self.state[4, -1] = state_[7]   # buffer size : s/10
        self.state[5, -1] = state_[8]   # last quality %

    def add_action(self, quality):
        self.quality = quality
        self.action_vec = np.zeros(A_DIM)
        self.action_vec[self.quality] = 1
        self.a_batch.append(self.action_vec)

    def add_state(self, state):
        self.s_batch.append(state)

    def add_reward(self, reward):
        self.r_batch.append(reward)

    def add_entropy(self, entropy):
        self.entropy.append(entropy)

    def add_old_pi(self, prob):
        self.old_pi_batch.append(prob)

    def clear_content(self):
        self.a_batch = []
        self.s_batch = []
        self.r_batch = []
        self.entropy = []
        self.old_pi_batch = []

    def end_of_video(self):
        self.quality = 0
        self.last_quality = 0
        self.action_vec = np.zeros(A_DIM)
        self.action_vec[self.quality] = 1
        self.state = np.zeros((S_INFO, S_LEN))

        self.s_batch.append(self.state)
        self.a_batch.append(self.action_vec)
        self.old_pi_batch.append(self.action_vec)




